package hongcaosp.app.android.user.member;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blackchopper.loginshare.messager.Messager;
import com.bumptech.glide.Glide;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.UserInfo;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.GlideUtils;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.view.CircleImageView;

public class MemberActivity extends BaseActivity implements IMemberDateView,IWXAPIEventHandler {
    private MemberLayout memberLayout;
    private ImageView userLogo;
    private TextView userNameTV,descTV,lastPriceTV,currentPriceTV;
    private MemberDatePresenter memberDatePresenter;

    private IMemberDate currentDate;
    @Override
    protected void initData(Intent intent) {
        long userId = intent.getLongExtra("user_id",0);

        memberDatePresenter = new MemberDatePresenter(this);
        memberDatePresenter.getList();
        memberDatePresenter.getUserData(userId);

    }

    @Override
    protected void initView() {
        Messager.getInstance().register(this);
        StatusBarUtil.setStatusBarDarkTheme(this,false);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        memberLayout = findViewById(R.id.menu_layout);
        userLogo = findViewById(R.id.user_logo);
        userNameTV = findViewById(R.id.user_name);
        descTV = findViewById(R.id.des_tv);
        lastPriceTV = findViewById(R.id.less_price);
        lastPriceTV.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
        currentPriceTV = findViewById(R.id.total_price);
        findViewById(R.id.buy_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentDate == null) {
                    ToastManager.getInstance().showToast("还没有选择会员套餐");
                    return;
                }
                memberDatePresenter.pay();
            }
        });
        memberLayout.setMemberItemSelectListener(new MemberItemHolder.MemberItemSelectListener() {
            @Override
            public void onItemSelected(MemberItemHolder memberItemHolder) {
                currentDate = memberItemHolder.getDate();
                freshData();
            }
        });
    }

    private void freshData() {
        if (currentDate != null) {
            memberDatePresenter.chargeMoneyId = currentDate.getId();
            currentPriceTV.setText("总计：¥" + currentDate.getCurrentPrice());
            lastPriceTV.setText("¥" + currentDate.getLastPrice() + "");
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_member;
    }

    @Override
    public void freshList(List<IMemberDate> dateList) {
        memberLayout.initDates(dateList);
    }

    @Override
    public void updateUser(UserInfo user, String desc) {
        if (user != null) {
            GlideUtils.loadCircleImage(this,user.getUserLogo(),userLogo,R.drawable.icon_head);
            userNameTV.setText(user.getUserName());
        }
        descTV.setText(desc);
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Messager.getInstance().unRegister(this);
    }

    //====支付回调
    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp == null) {
            ToastManager.getInstance().showToast("未知错误");
            return;
        }
        if (baseResp.errCode == 0) {//支付成功
            ToastManager.getInstance().showToast("支付成功");
            finish();
            //成功逻辑

        } else if (baseResp.errCode == -2) {//用户取消支付
            ToastManager.getInstance().showToast("支付已取消");
        } else { //支付失败
            ToastManager.getInstance().showToast("支付失败");
        }
    }
}
